﻿@model IEnumerable<PessModel.PAuthor>

@{
   
    bool allowEdit = false;
    if (ViewBag.AllowEdit != null && ViewBag.AllowEdit == true)
    {
        allowEdit = true;
    }

    var PAuthors = Model.OrderByDescending(pa => pa.Type).ThenBy(pa=>pa.Order);
    var largestorder = 0;
    PessModel.PAuthor pauthor = ViewBag.DefaultAuthor;
    string Format = ViewBag.Format;
    
}

<div class="displayConScroe" style="margin-top:-15px;padding-top:15px; padding-bottom:15px;width:700px">
    <table id="pauthorstable">
        <tr>
            <th>
            </th>
            <th>
                Email Address
            </th>
            <th>
                Name
            </th>
            <th>
                Affiliation
            </th>
            <th>
                Country
            </th>
            <th>
                Role
            </th>
@*            <th>
                Order
            </th>*@
                        @if (allowEdit)
                        {
                <th style="width:150px; "></th>
                        }
        </tr>

        @for (int i = 0; i < PAuthors.Count(); i++)
        {
            <tr>
                <td>#@(i + 1)</td>
                <td>
                    @Html.DisplayFor(modelItem => PAuthors.ElementAt(i).EMail)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => PAuthors.ElementAt(i).Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => PAuthors.ElementAt(i).Organization)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => PAuthors.ElementAt(i).Country)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => PAuthors.ElementAt(i).TypeName)
                </td>
@*                <td>
                    @Html.DisplayFor(modelItem => PAuthors.ElementAt(i).Order)
                </td>*@
                @if (allowEdit)
                {
                <td style="text-align:left; width:100px">
                    <input type="button" class="pauther_delete" title="@PAuthors.ElementAt(i).ID" value="Delete" />
                    @if (i > 0)
                    { 
                        <input type="button" class="moveup" title="@PAuthors.ElementAt(i).ID" id="@PAuthors.ElementAt(i - 1).ID" value="&uarr;" />
                    }
                    @if (i < (Model.Count() - 1))
                    {
                        <input type="button" class="movedown" title="@PAuthors.ElementAt(i).ID" id="@PAuthors.ElementAt(i + 1).ID" value="&darr;" />
                    }
                </td>
                }
            </tr>
                if (i == PAuthors.Count() - 1)
                {
                    largestorder = PAuthors.ElementAt(i).Order;
                }
        }
        </table>
        </div>
<input type="hidden" value="@(largestorder)" id="largestOrder" />
    <input type="button" value="Add Author #@(PAuthors.Count() + 1)" id="btnAddNewAuthor"/>


    <div id="AuthorForm" style="margin-left:15px;display:none">
        <div class="editor-label">
            Email
        </div>
        <div class="editor-field" >
            <input type="email" value="" id="PEMail" name="PEMail" required/> *
            <span class="field-validation-valid" data-valmsg-for="PEMail" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label" >
            Last Name
        </div>
        <div class="editor-field" >
            <input type="text" value="" id="PLastName" name="PLastName" required /> *
            <span class="field-validation-valid" data-valmsg-for="PLastName" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label" >
            First Name
        </div>
        <div class="editor-field" >
            <input type="text" value="" id="PFirstName" name="PFirstName" required /> *
            <span class="field-validation-valid" data-valmsg-for="PFirstName" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label" >
            Affiliation
        </div>
        <div class="editor-field" >
            <input type="text" value="" id="POrganization" name="POrganization" required /> *
            <span class="field-validation-valid" data-valmsg-for="POrganization" data-valmsg-replace="true"></span>
        </div>

        <div class="editor-label" >
            Country
        </div>
        <div class="editor-field" >
            <input type="text" value="" id="PCountry" name="PCountry" required /> *
            <span class="field-validation-valid" data-valmsg-for="PCountry" data-valmsg-replace="true"></span>
        </div>
        
        <div class="editor-label" >
            Role
        </div>
        <div class="editor-field" >
            <select id="authortype" name="authortype">
            @if (Format == "01")
            {
                <option value="2">Chair</option>
                <option value="1" selected>Presenter</option>
            }
            else
            {
                <option value="0" selected>Author</option>
            }
            </select>
        </div>

            @Html.Hidden("PArchiveID", pauthor.ArchiveID)

        <div style="clear:both; margin-left:150px">
            <input type="button" value="Save" id="AddPAuthor" />
            <input type="button" value="Cancel" id="CancelAddPAuthor" />
        </div>

    </div>

<script type="text/javascript">
    $("#btnAddNewAuthor").click(function (event) {
        $("#AuthorForm").attr({ style: "display:" });
    });

    $("#CancelAddPAuthor").click(function (event) {
        $("#AuthorForm").attr({ style: "display:none" });
    });

    $("#AddPAuthor").click(function (event) {
        //alert($("#authortype").val());
        var validator = $("form").validate();

        if(validator.element("#PEMail")
            &&validator.element("#PLastName")
            &&validator.element("#PFirstName")
            && validator.element("#POrganization")
            && validator.element("#PCountry")
           &&validator.element("#authortype")) {
            $.ajax({
                url: "@Url.Action("CreateAjax", "PAuthor")" ,
                type: "POST",
                data: {
                    archiveid: $("#PArchiveID").val(),
                    email: $("#PEMail").val(),
                    lastname: $("#PLastName").val(),
                    firstname: $("#PFirstName").val(),
                    country: $("#PCountry").val(),
                    organization: $("#POrganization").val(),
                    order: parseInt($("#largestOrder").val()) + 1,
                    authortype: $("#authortype").val()
                },
                success: function (pauthors) {
                    createTable(pauthors);
                }
            });
        }
        else
        {
            //$(".error").attr(style = "display:");
        }
    });

    $("input.pauther_delete").click(function (event) {
        DeletePauthor(this.title);
    });

    $("input.moveup").click(function (event) {
        MoveAuthor(this.title,this.id);
    });

    $("input.movedown").click(function (event) {
        MoveAuthor(this.title,this.id);
    });

    function DeletePauthor(id) {
        //alert(id);
        $.ajax({
            url: "@Url.Action("DeleteAjax", "PAuthor")",
            type: "POST",
            data: {
                id: id
            },
            success: function (pauthors) {
                createTable(pauthors);
            }
        });
    }

    function MoveAuthor(id, nid) {
        //alert(id + "," + nid);
        $.ajax({
            url: "@Url.Action("MoveAjax", "PAuthor")",
            type: "POST",
            data: {
                id: id, nid:nid
            },
            success: function (pauthors) {
                createTable(pauthors);
            }
        });
    }

    function createTable(pauthors) {
        $("#pauthorstable").empty();
        $("<tr>").html("<th></th><th>Email </th><th>Name</th><th>Affiliation</th><th>Country </th><th>Role</th><th style=\"width:150px; \"></th>").appendTo("#pauthorstable");
        var count = pauthors.length + 1;
        var largest=0;
        for (var i = 0; i < pauthors.length; i++)
        {
            shtml = "<td>#" + (i + 1) + "</td><td>" + pauthors[i].Email + "</td><td>" + pauthors[i].Name + "</td><td>" + pauthors[i].Organization + "</td><td>" + pauthors[i].Country + "</td><td>";
            switch (pauthors[i].Type) {
                case 0:
                    shtml +="Author";
                    break;
                case 1:
                    shtml +="Presenter"
                    break;
                case 2:
                    shtml +="Chair"
                    break;
                default:
                    shtml += pauthors[i].Type;
            }
            
            shtml+="</td><td style=\"text-align:left; width:100px\"><input type=\"button\" onclick=\"DeletePauthor(this.title)\" class=\"pauther_delete\" title=\"" + pauthors[i].ID + "\" value=\"Delete\"> ";
            if (i > 0)
                shtml += "<input type=\"button\" onclick=\"MoveAuthor(this.title,this.id)\"  class=\"moveup\" title=\"" + pauthors[i].ID + "\" id=\"" + pauthors[i - 1].ID + "\" value=\"&uarr;\" /> ";
            if (i < pauthors.length - 1)
                shtml += "<input type=\"button\" onclick=\"MoveAuthor(this.title,this.id)\"  class=\"movedown\" title=\"" + pauthors[i].ID + "\" id=\"" + pauthors[i + 1].ID + "\" value=\"&darr;\" /> ";
            shtml += "</td>";
            $("<tr>").html(shtml).appendTo("#pauthorstable");
            //alert(pauthor.ID);
            if(i==pauthors.length-1)
                largest=pauthors[i].Order;
        }
        $("#AuthorForm").attr({ style: "display:none" });
        $("#PEMail").val("");
        $("#PLastName").val("");
        $("#PFirstName").val("");
        $("#PCountry").val("");
        $("#POrganization").val("");
        $("#btnAddNewAuthor").val("Add Author #" + count);
        $("#largestOrder").val(largest);
    }

    //$("#AuthorForm").validate({
    //    submitHandler: function (form) {
    //        // alert("submitted!");
    //        form.submit();
    //    },
    //    invalidHandler: function (event, validator) {
    //        //alert("no score");
    //    }
    //});
</script>